package zhaolu.com.example.Delicacy.Fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import zhaolu.com.example.Delicacy.Entity.Dish;
import zhaolu.com.example.Delicacy.MySQL.DishDAO;
import zhaolu.com.example.Delicacy.R;
import zhaolu.com.example.Delicacy.Utils.CommonUtils;

public class OrderFragment extends Fragment implements AdapterView.OnItemClickListener {
    private ListView listView;
    private SimpleAdapter simpleAdapter;
    private List<Map<String, Object>> list;
    private DishDAO dishDAO;
    private List<Dish> dishList;
    private int[] image = {
            R.drawable.dish1, R.drawable.dish2,
            R.drawable.dish3, R.drawable.dish4,
            R.drawable.dish5, R.drawable.dish6,
            R.drawable.dish7, R.drawable.dish8,
            R.drawable.dish9, R.drawable.dish10,
            R.drawable.dish11, R.drawable.dish12,
            R.drawable.dish13
    };

    @Nullable
    @Override
    public View onCreateView(@Nullable LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.order_fragment, container, false);
        listView = view.findViewById(R.id.orderList);
        dishDAO = new DishDAO();
        dishList = new ArrayList<>();
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                dishList = dishDAO.getDishList();
            }
        });
        thread.start();
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // 如何让MySQL查询线程先进行？join()
        list = new ArrayList<>();
        for (int i = 0; i < 13; i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("image", image[i]);
            map.put("title", dishList.get(i).getName());
            map.put("description", "\n单价：" + dishList.get(i).getPrice() + "元");
            list.add(map);
        }
        simpleAdapter = new SimpleAdapter(
                getActivity(), list,
                R.layout.order_menu,
                new String[]{"image", "title", "description"},
                new int[]{R.id.orderMenu_image, R.id.orderMenu_title, R.id.orderMenu_description});
        listView.setAdapter(simpleAdapter);
        listView.setOnItemClickListener(this);
        return view;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Map<String, Object> map = list.get(position);
        String title = (String) map.get("title");
        CommonUtils.showShortMsg(getActivity(), "点击了" + title);
    }
}
